package com.corner.system.service.impl;

import com.corner.common.exception.GlobalException;
import com.corner.system.domain.Approval;
import com.corner.system.domain.Subsidy;
import com.corner.system.domain.SubsidyTemp;
import com.corner.system.mapper.ApprovalMapper;
import com.corner.system.mapper.SubsidyMapper;
import com.corner.system.mapper.SubsidyTempMapper;
import com.corner.system.service.SubsidyTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: corner-admin
 * @description: 补贴审批表服务类
 * @author: lydms
 * @create: 2023-11-09 09:37
 **/
@Service
public class SubsidyTempServiceImpl implements SubsidyTempService {


    @Autowired
    private SubsidyTempMapper subsidyTempMapper;
    @Autowired
    private SubsidyMapper subsidyMapper;
    @Autowired
    private ApprovalMapper approvalMapper;

    @Override
    public List<SubsidyTemp> selectTempList(SubsidyTemp subsidyTemp) throws Exception {
        if(subsidyTemp.getApprovalId() == null){
            throw new GlobalException("审批编号不能为空！");
        }
        List<SubsidyTemp> subsidyTemps = subsidyTempMapper.selectTempList(subsidyTemp);
        return subsidyTemps;
    }

    @Override
    public Map getStatisticsData(String belongMonth,Long approvalId) throws Exception{
        // 通过总额(实际补贴金额)
        // 通过总数
        // 拒绝总额
        // 拒绝总笔数
        // 户均电费
        // 审批编号
        // 审批账号
        // 审批签字人
        // 单据创建时间
        // 导出时间
        // 审批时间
        // 城市名称
        // 地区名称
        Map statisticsData = subsidyTempMapper.getStatisticsData(belongMonth,approvalId);
        if(statisticsData == null){
            statisticsData = new HashMap();
        }
        //已领取总金额
        // 已领取总人数
        Map map = subsidyMapper.selectStatisticsDataByApprovalId(approvalId);
        statisticsData.putAll(map);

        //获取上个月数据
        //上个月份
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");
        Date parse = sd.parse(belongMonth);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        calendar.add(Calendar.MONTH,-1);
        Date time = calendar.getTime();
        String format = sd.format(time);
        System.out.println(format);

        Approval approval = approvalMapper.selectApprovalById(approvalId);
        Approval approval1 = new Approval();
        approval1.setAreaPath3(approval.getAreaPath3());
        approval1.setBelongMonth(format);
        List<Approval> approvals = approvalMapper.selectApprovalList(approval1);
        if(approvals.size() > 0){
            Approval approval2 = approvals.get(0);
            Map before = subsidyMapper.selectStatisticsDataByApprovalId(approval2.getApprovalId());
            statisticsData.put("beforeMonth",format);
            statisticsData.put("beforeSumAmount",before.get("sumAmount"));
            statisticsData.put("beforeSumCount",before.get("sumCount"));
            statisticsData.put("beforeFailAmount",before.get("failAmount"));
            statisticsData.put("beforeFailCount",before.get("failCount"));
        }else{
            statisticsData.put("beforeMonth","");
            statisticsData.put("beforeSumAmount",0);
            statisticsData.put("beforeSumCount",0);
            statisticsData.put("beforeFailAmount",0);
            statisticsData.put("beforeFailCount",0);
        }

        return statisticsData;
    }


    public static void main(String[] args) throws Exception{
        String bm = "2024-01";
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");
        Date parse = sd.parse(bm);
        System.out.println(parse);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        calendar.add(Calendar.MONTH,-1);
//        int year = calendar.get(Calendar.YEAR);
//        int month = calendar.get(Calendar.MONTH) + 1;
//
//        System.out.println(year);
//        System.out.println(month);

        Date time = calendar.getTime();
        String format = sd.format(time);
        System.out.println(format);

        //String formattedNum = String.format("%02d", month); // 将数字转换为两位数并在个位数前面补零
        //System.out.println(formattedNum); // 输出结果为 "05"

        // String[] split = bm.split("-");



    }




}
